package cac.fenjian.resource.mstaskpackagebasic.rest;

import cac.fenjian.resource.AoTask.MsTaskPackage.dto.MsTaskPackageDTO;
import cac.fenjian.resource.mstaskpackagebasic.service.MsTaskPackageBasicService;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@RequestMapping("/msTaskPackageBasic/")
public class MsTaskPackageBasicRest {

    @Autowired
    private MsTaskPackageBasicService msTaskPackageBasicService;

    /*
    条件查询
     */
    @GetMapping("/search/{ms_task_package_id}")
    public ResponseMsg<MsTaskPackageDTO> search(@PathVariable ("ms_task_package_id") String msTaskPackageId){
        ResponseMsg<MsTaskPackageDTO> responseMsg=new ResponseMsg<>();
        MsTaskPackageDTO msTaskPackageDTO = msTaskPackageBasicService.selectById(msTaskPackageId);
        if(msTaskPackageDTO!= null ){
            responseMsg.setResponseBody(msTaskPackageDTO);
        }
        return responseMsg;
    }

    //按条件分页查询
    @PostMapping("/searchByPage")
    public ResponseMsg<QueryRespBean<MsTaskPackageDTO>> searchByPage(@RequestBody QueryReqBean<MsTaskPackageDTO> queryReqBean) {
        ResponseMsg<QueryRespBean<MsTaskPackageDTO>> responseMsg = new ResponseMsg<>();
        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), MsTaskPackageDTO[].class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            MsTaskPackageDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<MsTaskPackageDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<MsTaskPackageDTO> result = msTaskPackageBasicService.searchBasicByPage(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //新增
    @PostMapping("/save")
    public ResponseMsg<String> save(@RequestBody MsTaskPackageDTO msTaskPackageDTO) throws ParseException {
        ResponseMsg<String> responseMsg=new ResponseMsg<>();
        msTaskPackageDTO.setMsTaskPackageId(ContextCommonHolder.getOrgId());
        if(StringUtils.isEmpty(msTaskPackageDTO.getMsTaskPackageId())){
            responseMsg.setResponseBody(msTaskPackageBasicService.insertBasic(msTaskPackageDTO));
        }else {
            responseMsg.setResponseBody(String.valueOf(msTaskPackageBasicService.updateBasic(msTaskPackageDTO)));
        }
        return responseMsg;
    }

    //批量新增
    @PostMapping("/saveList")
    public ResponseMsg<Integer> save2(@RequestBody List<MsTaskPackageDTO> msTaskPackageDTOList){
        ResponseMsg<Integer> responseMsg =new ResponseMsg<>();
        responseMsg.setResponseBody(msTaskPackageBasicService.insertBasicList(msTaskPackageDTOList));
        return responseMsg;
    }

    //修改部分字段
    @PostMapping("/update")
    public ResponseMsg<Integer> updateBasic(@RequestBody MsTaskPackageDTO msTaskPackageDTO) throws ParseException {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msTaskPackageBasicService.updateBasic(msTaskPackageDTO));
        return responseMsg;
    }

    //批量更新
    @PostMapping("/updateList")
    public ResponseMsg<Integer> updateBasicList(@RequestBody List<MsTaskPackageDTO> MsTaskPackageDTOList){
        ResponseMsg<Integer> responseMsg =new ResponseMsg<>();
        responseMsg.setResponseBody(msTaskPackageBasicService.updateBasicList2(MsTaskPackageDTOList));
        return responseMsg;
    }

    //按主键单条删除
    @PostMapping("/deleteById")
    public ResponseMsg<Integer> deleteById( @RequestBody String msTaskPackageId) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msTaskPackageBasicService.deleteBasic(msTaskPackageId));
        return responseMsg;
    }

    @PostMapping("/deleteByIds")
    //批量删除
    public ResponseMsg<Integer> deleteByIds(@RequestBody String[] ids) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msTaskPackageBasicService.deleteBasicByIds(ids));
        return responseMsg;
    }
}
